﻿using System;
using System.Windows.Forms;
using Buccaneer.Types;
using System.IO;
using System.Threading;
using System.Drawing;
using System.Diagnostics;
using Buccontrols;

namespace Buccaneer
{
    /// <summary>
    /// Holds A static instance of global program shtuff
    /// </summary>
    public class Global
    {
        public Global() { } //Constructor

        #region Plugins

        public static bool LoadPlugins()
        {
            // Call the find plugins routine, to search in our Plugins Folder. return if folder was not found.
            try
            {
                MainProgram.Plugins.FindPlugins(Options.PluginsFolder);
            }
            catch (DirectoryNotFoundException) { Options.WriteComment("(!) There are errors found!"); return false; }

            Options.WriteComment("(i) There are " + MainProgram.Plugins.AvailablePlugins.Count.ToString() + " plugins found:");

            foreach (Types.AvailablePlugin pluginOn in MainProgram.Plugins.AvailablePlugins)
            {
                Options.WriteComment(" -  " + pluginOn.Instance.Name);
            }
            return true;
        }

        public static bool FindPlugin(string name, ref Panel target)
        {
            AvailablePlugin selectedPlugin = MainProgram.Plugins.AvailablePlugins.Find(name);

            // Return false if the plugin was not found
            if (selectedPlugin == null) return false;
            // Clear visuals: doesn't close the instance of the plugin
            target.Controls.Clear();

            // Set the dockstyle of the plugin to fill, to fill up the space provided
            selectedPlugin.Instance.MainInterface.Dock = DockStyle.Fill;

            // Finally, add the usercontrol to the panel
            target.Controls.Add(selectedPlugin.Instance.MainInterface);

            Options.WriteComment("(i) Plugin " + name + "succesfully loaded!");
            return true;
        }

        #endregion

        #region ThreadManaging

        public static bool EnableCommandTyping()
        {
            Thread ct = new Thread(
                new ThreadStart(
                delegate()
                {
                    string readline;
                    string command;
                    string[] args = null;

                    while (true)
                    {
                        readline = Console.ReadLine();
                        command = readline.Split('(')[0];
                        args = null;
                        try { args = readline.Split('(')[1].Split(','); }
                        catch (Exception) { args = new string[] { "" }; }
                        
                        MainProgram.Plugins.Act(command, args);
                        try
                        {
                        }
                        catch (Exception e)
                        {
                            Options.WriteComment("(X) Error: " + e.Message);
                        }
                    }
                }));
            ct.Start();
            return true;
        }

        #endregion

        #region Options

        public static bool LoadOptions()
        {


            return true;
        }

        #endregion

        #region Functions

        public static bool Navigate(string path)
        {
            Options.WriteComment("(i) Navigating to " + path);
            string lastPath = MainProgram.selectedPlugin.Instance.Path;
            if (!MainProgram.selectedPlugin.Instance.Navigate(path))
            {
                Options.WriteComment("(i) Navigating failed");
                Options.WriteComment("(i) Navigating to " + lastPath);
                MainProgram.selectedPlugin.Instance.Path = lastPath;
            }
            return true;
        }

        #endregion

    }
}
